package com.chaosj.memory.handlers;

import com.chaosj.memory.dto.RandomLexiconDto;
import com.google.common.base.Splitter;

import java.util.List;
import java.util.Locale;

/**
 * @className: RandomLexiconHandler
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/13
 **/
public class RandomLexiconHandler extends AbstractToolHandler<RandomLexiconDto>{

    @Override
    public String name() {
        return "rd";
    }

    @Override
    public void handler(RandomLexiconDto req, StringBuffer outBuffer) {
        if( "c".equalsIgnoreCase(req.getCmd()) ) {
            if( "all".equalsIgnoreCase(req.getLexicon()) ){
                formatter( getResourceContent(String.format("lexicon/c/",req.getLexicon()), req.getNumber(), " "), outBuffer );
            }else{
                formatter( getResourceContent(String.format("lexicon/c/THUOCL_%s.txt",req.getLexicon()), req.getNumber(), " "), outBuffer );
            }
        }else if( "e".equalsIgnoreCase(req.getCmd()) ) {
            getResourceContent(String.format("lexicon/e/%s.output",req.getLexicon()), req.getNumber(), "|=|").forEach(line->{
                formatterE(line, outBuffer);
            });
        } else if( "x".equalsIgnoreCase(req.getCmd()) ) {
            getResourceContent(String.format("lexicon/x/%s.output",req.getLexicon()), req.getNumber(), "|=|").forEach(line->{
                formatterE(line, outBuffer);
            });
        } else if( "u".equalsIgnoreCase(req.getCmd()) ){

        } else {

        }
    }

    @Override
    public RandomLexiconDto cmd2Dto(String[] args) {
        return new RandomLexiconDto(args);
    }

    private void formatterE(String line, StringBuffer buffer) {
        if( line.contains("=") ){
            List<String> lists = Splitter.on("=").splitToList(line);
            String[] splitters = lists.get(0).split("<->");
            buffer.append(splitters[0]).append("\n");
            buffer.append(splitters[1]).append("\n");
            buffer.append(lists.get(1)).append("\n\n");
        }
    }
}
